package com.allinpokers.vest.allinvest.model.respon;

import com.allinpokers.vest.allinvest.model.request.PageReq;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

@Data
@Accessors(chain = true)
public class PageBean<T> {
    @ApiModelProperty(value = "总记录数量")
    private long total = 0;
    @ApiModelProperty(value = "总页数")
    private int pages = 0;
    @ApiModelProperty(value = "是否最后一页")
    private boolean isLastPage = true;
    @ApiModelProperty(value = "列表信息")
    protected List<T> beanList = new ArrayList<>();


    public PageBean<T> set(long total, PageReq req, List<T> beanList) {
        return set(total, req.getPage(), req.getSize(), beanList);
    }

    public PageBean<T> set(long total, Integer page, Integer size, List<T> beanList) {
        this.total = total;
        //计算总页数
        if (page != null && size != null && size > 0) {
            long pageCount = total / size + (total % size == 0 ? 0 : 1);
            this.pages = (int) pageCount;
            this.isLastPage = this.pages == page;
        }
        this.beanList = beanList;
        return this;
    }

    public static <T> PageBean<T> empty() {
        return of(0, 0, 10, new ArrayList<>());
    }

    /**
     * 生成PageBean
     *
     * @param total    总记录数
     * @param page     当前页
     * @param size     页大小
     * @param beanList 列表集合
     * @param <T>      泛型
     * @return page
     */
    public static <T> PageBean<T> of(long total, Integer page, Integer size, List<T> beanList) {
        return new PageBean<T>().set(total, page, size, beanList);
    }
}
